/*
 * code_engine.h
 *
 *  Created on: 2011-7-19
 *      Author: minorityee1984@gmail.com
 *     Purpose: 編解碼實現
 *        Note: 1.使用前必須首先調用init_code_engine()初始化
 *              2.所有編解碼“函數”的pszBuf字段由調用者計算偏移量
 *                puOffset字段傳出相對於傳入的pszBuf的偏移量
 *                所有編解碼“宏定義”的offset字段則與之前的offset累加
 */

#ifndef CODE_ENGINE_H_
#define CODE_ENGINE_H_

#include "type_def.h"
#include "msg_def.h"
#include <string.h>
#include <stdio.h>


extern int g_code_endian;

#define IS_BIG_ENDIAN()	g_code_endian

#ifdef DEBUG

#define ENCODE(pszBuf, offset, value) do { \
	printf("start ENCODE\n");\
	Byte *pb = (Byte*)&(value);	\
    unsigned int i = sizeof(value); \
    printf("sizeof(value=0x%x)=%d\n", value, i); \
    assert (0 != i); \
	if (IS_BIG_ENDIAN())	\
	{	\
		printf("big-endian\n"); \
		memcpy(pszBuf, &(value), sizeof(value));	\
	}	\
	else 	\
	{	\
		printf("little-endian\n"); \
		while (i-- > 0)\
		{\
			printf("pb[%d]=0x%x\n", i, pb[i]);\
			*pszBuf++ = pb[i];		\
		}\
	}\
	offset += sizeof(value);	\
}while(0)

#define DECODE(pszBuf, offset, value) do { \
	printf("start DECODE\n");\
	Byte *pb = (Byte*)&(value);	\
	unsigned int i = sizeof(value); \
	printf("sizeof(value=0x%x)=%d\n", value, i); \
	assert (0 != i); \
	if (IS_BIG_ENDIAN())	\
	{	\
		memcpy(&(value), pszBuf, sizeof(value));	\
	}	\
	else 	\
		{	\
			while( i-- > 0)	\
			{\
				printf("pszBuf[%d]=0x%x\n", i, pszBuf[i]);\
				pb[i] = *pszBuf++;		\
			}\
		}\
		offset += sizeof(value);	\
}while(0)

#else

#define ENCODE(pszBuf, offset, value) do { \
	Byte *pb = (Byte*)&(value);	\
    unsigned int i = sizeof(value); \
    assert (0 != i); \
	if (IS_BIG_ENDIAN())	\
	{	\
		memcpy(pszBuf, &(value), sizeof(value));	\
	}	\
	else 	\
	{	\
		while (i-- > 0)\
		{\
			*pszBuf++ = pb[i];		\
		}\
	}\
	offset += sizeof(value);	\
}while(0)

#define DECODE(pszBuf, offset, value) do { \
	Byte *pb = (Byte*)&(value);	\
	unsigned int i = sizeof(value); \
	assert (0 != i); \
	if (IS_BIG_ENDIAN())	\
	{	\
		memcpy(&(value), pszBuf, sizeof(value));	\
	}	\
	else 	\
		{	\
			while( i-- > 0)	\
			{\
				pb[i] = *pszBuf++;		\
			}\
		}\
		offset += sizeof(value);	\
}while(0)

#endif




typedef int (*msg_encode_method)(Byte *, unsigned int, unsigned int*, const void*, unsigned int);
typedef int (*msg_decode_method)(const Byte *, unsigned int, unsigned int*, void*, unsigned int);

//初始化code engine
void init_code_engine();


int encode_msg_header(
				Byte 				*pszBuf,
				unsigned int 		uBufLen,
				unsigned int 		*puOffset,
				const MsgHeader 	*pstMsgHeader,
				unsigned int 		uSizeOfMsgHeader);

int decode_msg_header(
				const 				Byte *pszBuf,
				unsigned int 		uBufLen,
				unsigned int		*puOffset,
				MsgHeader 			*pstMsgHeader,
				unsigned int 		uSizeOfMsgHeader);


int encode_msg(
				Byte				*pszBuf,
				unsigned int		uBufLen,
				unsigned int		*puOffset,
				const Message		*pstMessage,
				unsigned int		uSizeOfMsg);

int decode_msg(
				const Byte			*pszBuf,
				unsigned int		uBufLen,
				unsigned int		*puOffset,
				Message				*pstMessage,
				unsigned int		uSizeOfMsg);

#endif /* CODE_ENGINE_H_ */
